/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package gui;

import bn.BNode;
import dat.EnumVariable;
import bn.Predef;
import dat.Variable;
import com.mxgraph.view.mxGraph;
import java.awt.Dimension;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFileChooser;

/**
 *
 * @author mikael
 */
public class MainJFrame extends javax.swing.JFrame {

    BNContainer bnc;
    
    /**
     * Creates new form MainJFrame
     */
    public MainJFrame() {
        this.setSize(dimension);
        this.setPreferredSize(dimension);
        initComponents();
        bnc = new BNContainer();
        myGraphPanel.setBNContainer(bnc);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        myAddNodeDialog = new javax.swing.JDialog();
        myNameLabel = new javax.swing.JLabel();
        myNameTextField = new javax.swing.JTextField();
        myTypeComboBox = new javax.swing.JComboBox();
        myTypeLabel = new javax.swing.JLabel();
        myParamsTextField = new javax.swing.JTextField();
        myParamsLabel = new javax.swing.JLabel();
        myConfirmButton = new javax.swing.JButton();
        myCancelButton = new javax.swing.JButton();
        myAddErrorMessage = new javax.swing.JDialog();
        myAddErrorMessagePanel = new javax.swing.JPanel();
        myAddErrorMessageLabel = new javax.swing.JLabel();
        myAddErrorMessageOKButton = new javax.swing.JButton();
        myFileDialog = new javax.swing.JDialog();
        myFileChooser = new javax.swing.JFileChooser();
        myGraphPanel = new gui.MyGraphPanel();
        myMenuBar = new javax.swing.JMenuBar();
        myFileMenu = new javax.swing.JMenu();
        myLoadMenuItem = new javax.swing.JMenuItem();
        mySaveMenuItem = new javax.swing.JMenuItem();
        myRenderMenuItem = new javax.swing.JMenuItem();
        myEditMenu = new javax.swing.JMenu();
        myAddMenuItem = new javax.swing.JMenuItem();
        mySelectMenuItem = new javax.swing.JCheckBoxMenuItem();
        myDeleteMenuItem = new javax.swing.JCheckBoxMenuItem();
        jMenu1 = new javax.swing.JMenu();
        myHierarchicalMenuItem = new javax.swing.JRadioButtonMenuItem();
        myParallelEdgeMenuItem = new javax.swing.JRadioButtonMenuItem();
        myPartitionMenuItem = new javax.swing.JRadioButtonMenuItem();

        myAddNodeDialog.setTitle("Add Node");

        myNameLabel.setText("Name:");

        myNameTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                myNameTextFieldActionPerformed(evt);
            }
        });

        myTypeComboBox.setModel(new DefaultComboBoxModel(bn.Predef.getVariableTypes()));
        myTypeComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                myTypeComboBoxActionPerformed(evt);
            }
        });

        myTypeLabel.setText("Type:");

        myParamsLabel.setText("Params:");

        myConfirmButton.setText("Confirm");
        myConfirmButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                myConfirmButtonActionPerformed(evt);
            }
        });

        myCancelButton.setText("Cancel");
        myCancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                myCancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout myAddNodeDialogLayout = new javax.swing.GroupLayout(myAddNodeDialog.getContentPane());
        myAddNodeDialog.getContentPane().setLayout(myAddNodeDialogLayout);
        myAddNodeDialogLayout.setHorizontalGroup(
            myAddNodeDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(myAddNodeDialogLayout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(myAddNodeDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(myNameLabel)
                    .addComponent(myTypeLabel)
                    .addComponent(myParamsLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(myAddNodeDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(myAddNodeDialogLayout.createSequentialGroup()
                        .addComponent(myCancelButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(myConfirmButton))
                    .addGroup(myAddNodeDialogLayout.createSequentialGroup()
                        .addGroup(myAddNodeDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(myParamsTextField)
                            .addComponent(myTypeComboBox, 0, 281, Short.MAX_VALUE)
                            .addComponent(myNameTextField))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        myAddNodeDialogLayout.setVerticalGroup(
            myAddNodeDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(myAddNodeDialogLayout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addGroup(myAddNodeDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(myNameLabel)
                    .addComponent(myNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(myAddNodeDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(myTypeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(myTypeLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(myAddNodeDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(myParamsTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(myParamsLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(myAddNodeDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(myConfirmButton)
                    .addComponent(myCancelButton))
                .addContainerGap(8, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout myAddErrorMessagePanelLayout = new javax.swing.GroupLayout(myAddErrorMessagePanel);
        myAddErrorMessagePanel.setLayout(myAddErrorMessagePanelLayout);
        myAddErrorMessagePanelLayout.setHorizontalGroup(
            myAddErrorMessagePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        myAddErrorMessagePanelLayout.setVerticalGroup(
            myAddErrorMessagePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        myAddErrorMessageLabel.setText("Message");

        myAddErrorMessageOKButton.setText("OK");
        myAddErrorMessageOKButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                myAddErrorMessageOKButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout myAddErrorMessageLayout = new javax.swing.GroupLayout(myAddErrorMessage.getContentPane());
        myAddErrorMessage.getContentPane().setLayout(myAddErrorMessageLayout);
        myAddErrorMessageLayout.setHorizontalGroup(
            myAddErrorMessageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(myAddErrorMessageLayout.createSequentialGroup()
                .addGroup(myAddErrorMessageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(myAddErrorMessageLayout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(myAddErrorMessagePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(myAddErrorMessageLayout.createSequentialGroup()
                        .addGap(151, 151, 151)
                        .addGroup(myAddErrorMessageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(myAddErrorMessageOKButton)
                            .addComponent(myAddErrorMessageLabel))))
                .addGap(152, 152, 152))
        );
        myAddErrorMessageLayout.setVerticalGroup(
            myAddErrorMessageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, myAddErrorMessageLayout.createSequentialGroup()
                .addContainerGap(31, Short.MAX_VALUE)
                .addComponent(myAddErrorMessageLabel)
                .addGap(18, 18, 18)
                .addComponent(myAddErrorMessageOKButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(myAddErrorMessagePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        myFileDialog.setSize(new java.awt.Dimension(550, 400));

        myFileChooser.setDialogTitle("Load BN");
        myFileChooser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                myFileChooserActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout myFileDialogLayout = new javax.swing.GroupLayout(myFileDialog.getContentPane());
        myFileDialog.getContentPane().setLayout(myFileDialogLayout);
        myFileDialogLayout.setHorizontalGroup(
            myFileDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, myFileDialogLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(myFileChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        myFileDialogLayout.setVerticalGroup(
            myFileDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, myFileDialogLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(myFileChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        myGraphPanel.setPreferredSize(new java.awt.Dimension(1000, 800));

        myFileMenu.setText("File");

        myLoadMenuItem.setText("Load BN");
        myLoadMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                myLoadMenuItemActionPerformed(evt);
            }
        });
        myFileMenu.add(myLoadMenuItem);

        mySaveMenuItem.setText("Save BN");
        mySaveMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mySaveMenuItemActionPerformed(evt);
            }
        });
        myFileMenu.add(mySaveMenuItem);

        myRenderMenuItem.setText("Render");
        myRenderMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                myRenderMenuItemActionPerformed(evt);
            }
        });
        myFileMenu.add(myRenderMenuItem);

        myMenuBar.add(myFileMenu);

        myEditMenu.setMnemonic('E');
        myEditMenu.setText("<html><u>E</u>dit</html>");

        myAddMenuItem.setMnemonic('A');
        myAddMenuItem.setText("<html><u>A</u>dd Node</html>");
        myAddMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                myAddMenuItemActionPerformed(evt);
            }
        });
        myEditMenu.add(myAddMenuItem);

        mySelectMenuItem.setSelected(true);
        mySelectMenuItem.setText("Select");
        mySelectMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mySelectMenuItemActionPerformed(evt);
            }
        });
        myEditMenu.add(mySelectMenuItem);

        myDeleteMenuItem.setText("Delete");
        myDeleteMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                myDeleteMenuItemActionPerformed(evt);
            }
        });
        myEditMenu.add(myDeleteMenuItem);

        myMenuBar.add(myEditMenu);

        jMenu1.setText("Layout");

        myHierarchicalMenuItem.setSelected(true);
        myHierarchicalMenuItem.setText("Hierarchical");
        myHierarchicalMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                myHierarchicalMenuItemActionPerformed(evt);
            }
        });
        jMenu1.add(myHierarchicalMenuItem);

        myParallelEdgeMenuItem.setText("Parallel Edge");
        myParallelEdgeMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                myParallelEdgeMenuItemActionPerformed(evt);
            }
        });
        jMenu1.add(myParallelEdgeMenuItem);

        myPartitionMenuItem.setText("Partition");
        myPartitionMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                myPartitionMenuItemActionPerformed(evt);
            }
        });
        jMenu1.add(myPartitionMenuItem);

        myMenuBar.add(jMenu1);

        setJMenuBar(myMenuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(myGraphPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(myGraphPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void myLoadMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_myLoadMenuItemActionPerformed
        myFileDialog.setModal(true);
        myFileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
        myFileDialog.setLocationRelativeTo(this);
        myFileDialog.setVisible(true);
    }//GEN-LAST:event_myLoadMenuItemActionPerformed

    private void mySaveMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mySaveMenuItemActionPerformed
        myFileDialog.setModal(true);
        myFileChooser.setDialogType(JFileChooser.SAVE_DIALOG);
        myFileDialog.setLocationRelativeTo(this);
        myFileDialog.setVisible(true);
    }//GEN-LAST:event_mySaveMenuItemActionPerformed

    private void myAddMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_myAddMenuItemActionPerformed
        myAddNodeDialog.setModal(true);
        myAddNodeDialog.getRootPane().setDefaultButton(myConfirmButton);
        myAddNodeDialog.pack();
        myAddNodeDialog.setLocationRelativeTo(this);
        myAddNodeDialog.setVisible(true);
        setDeleteMode(false); // change to select mode if not already
        myNameTextField.requestFocus();
    }//GEN-LAST:event_myAddMenuItemActionPerformed

    private void myNameTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_myNameTextFieldActionPerformed
        // TODO add your handling code here:
        
    }//GEN-LAST:event_myNameTextFieldActionPerformed

    private void myCancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_myCancelButtonActionPerformed
        myAddNodeDialog.setVisible(false);
    }//GEN-LAST:event_myCancelButtonActionPerformed

    private void renderNetwork(BNContainer bnc) {
        final mxGraph graph = myGraphPanel.getGraph();
        graph.removeCells(myGraphPanel.getAllCells());
        for (BNode node : bnc.getBNet().getNodes()) {
            Variable var = node.getVariable();
            if (var != null) {
                String type = var.getPredef();
                String name = var.getName();
                String params = var.getParams();
                String color = (Predef.isEnumerable(type)?"yellow":"orange"); // yellow for enumerable nodes, orange for continuous
                String label = name;
                graph.getModel().beginUpdate();
                try {
                    Object newvertex = graph.insertVertex(graph.getDefaultParent(), null, label, 50, 50, 80, 30, "ROUNDED;strokeColor=black;fillColor="+color);
                    myGraphPanel.addVertex(name, newvertex);
                } finally {
                    graph.getModel().endUpdate();
                }
            }
        } // variables done... now connect them
        for (BNode node : bnc.getBNet().getNodes()) {
            String child_name = node.getVariable().getName();
            Object child_vertex = myGraphPanel.getVertex(child_name);
            if (node.getParents() != null) {
                for (EnumVariable parent : node.getParents()) {
                    String parent_name = parent.getName();
                    Object parent_vertex = myGraphPanel.getVertex(parent_name);
                    System.out.println("Inserting edge between" + parent_name + " and " + child_name); 
                    graph.getModel().beginUpdate();
                    try {
                        Object newedge = graph.insertEdge(graph.getDefaultParent(), null, "", parent_vertex, child_vertex);
                    } finally {
                        graph.getModel().endUpdate();
                    }
                }
            }
        }        
        myGraphPanel.executeLayout(getCurrentLayout());
    }
    
    private void myConfirmButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_myConfirmButtonActionPerformed
        final mxGraph graph = myGraphPanel.getGraph();
        Object parent = graph.getDefaultParent();
        // check if everything looks ok
        String name = myNameTextField.getText();
        String type = (String)(this.myTypeComboBox.getSelectedItem());
        String params = myParamsTextField.getText();
        String error_msg = "Failed to add node: Invalid variable specification";
        try {
            Variable var = Predef.getVariable(name, type, params);
            if (var != null) {
                String color = (Predef.isEnumerable(type)?"yellow":"orange"); // yellow for enumerable nodes, orange for continuous
                String label = name;
                graph.getModel().beginUpdate();
                try {
                    Object newvertex = graph.insertVertex(parent, null, label, 50, 50, 80, 30, "ROUNDED;strokeColor=black;fillColor="+color);
                    myGraphPanel.addVertex(name, newvertex);
                    bnc.addNode(Predef.getBNode(var, new ArrayList<Variable>(), Predef.getBNodeType(type)));
                } finally {
                    graph.getModel().endUpdate();
                }
                myNameTextField.setText("");
                myParamsTextField.setText("");
                myAddNodeDialog.setVisible(false);
                return;
            }
        } catch (RuntimeException e) {
            error_msg = e.getLocalizedMessage();
        }
        // something is wrong, report this?
        myAddErrorMessage.setModal(true);
        myAddErrorMessage.getRootPane().setDefaultButton(myAddErrorMessageOKButton);
        myAddErrorMessageLabel.setText(error_msg);
        myAddErrorMessage.pack();
        myAddErrorMessage.setLocationRelativeTo(myAddNodeDialog);
        myAddErrorMessage.setVisible(true);
    }//GEN-LAST:event_myConfirmButtonActionPerformed

    private void mySelectMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mySelectMenuItemActionPerformed
        deleteMode = !deleteMode;
        mySelectMenuItem.setState(!deleteMode);
        myDeleteMenuItem.setState(deleteMode);
    }//GEN-LAST:event_mySelectMenuItemActionPerformed

    public static boolean isDeleteMode() {
        return deleteMode;
    }

    public static void setDeleteMode(boolean deleteMode) {
        MainJFrame.deleteMode = deleteMode;
    }

    private void myDeleteMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_myDeleteMenuItemActionPerformed
        mySelectMenuItemActionPerformed(evt);
    }//GEN-LAST:event_myDeleteMenuItemActionPerformed

    private void myTypeComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_myTypeComboBoxActionPerformed
        String type = (String)(this.myTypeComboBox.getSelectedItem());
        myParamsTextField.setEnabled(Predef.isParameterised(type));
    }//GEN-LAST:event_myTypeComboBoxActionPerformed

    private void resetLayoutModes() {
        myHierarchicalMenuItem.setSelected(false);
        myParallelEdgeMenuItem.setSelected(false);
        myPartitionMenuItem.setSelected(false);
    }
    
    private int getCurrentLayout() {
        if (myParallelEdgeMenuItem.isSelected())
            return 1;
        else if (myPartitionMenuItem.isSelected())
            return 2;
        else
            return 0;
    }
    
    private void myHierarchicalMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_myHierarchicalMenuItemActionPerformed
        resetLayoutModes();
        myHierarchicalMenuItem.setSelected(true);
        myGraphPanel.executeLayout(0);
    }//GEN-LAST:event_myHierarchicalMenuItemActionPerformed

    private void myParallelEdgeMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_myParallelEdgeMenuItemActionPerformed
        resetLayoutModes();
        myParallelEdgeMenuItem.setSelected(true);
        myGraphPanel.executeLayout(1);
    }//GEN-LAST:event_myParallelEdgeMenuItemActionPerformed

    private void myPartitionMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_myPartitionMenuItemActionPerformed
        resetLayoutModes();
        myPartitionMenuItem.setSelected(true);
        myGraphPanel.executeLayout(2);
    }//GEN-LAST:event_myPartitionMenuItemActionPerformed

    private void myAddErrorMessageOKButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_myAddErrorMessageOKButtonActionPerformed
        myAddErrorMessage.setVisible(false);
    }//GEN-LAST:event_myAddErrorMessageOKButtonActionPerformed

    private void myFileChooserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_myFileChooserActionPerformed
        myFileDialog.setVisible(false);
        File file = myFileChooser.getSelectedFile();
        if (file != null) {
            if (myFileChooser.getDialogType() == JFileChooser.OPEN_DIALOG) {
                try {
                    bnc.load(file.getCanonicalPath());
                    renderNetwork(bnc);
                } catch (IOException ex) {
                    myAddErrorMessage.setModal(true);
                    myAddErrorMessage.getRootPane().setDefaultButton(myAddErrorMessageOKButton);
                    myAddErrorMessageLabel.setText(ex.getMessage());
                    myAddErrorMessage.pack();
                    myAddErrorMessage.setLocationRelativeTo(this);
                    myAddErrorMessage.setVisible(true);
                }
            } else { // SAVE
                try {
                    bnc.save(file.getCanonicalPath());
                } catch (IOException ex) {
                    myAddErrorMessage.setModal(true);
                    myAddErrorMessage.getRootPane().setDefaultButton(myAddErrorMessageOKButton);
                    myAddErrorMessageLabel.setText(ex.getMessage());
                    myAddErrorMessage.pack();
                    myAddErrorMessage.setLocationRelativeTo(this);
                    myAddErrorMessage.setVisible(true);
                }
            }
        }
    }//GEN-LAST:event_myFileChooserActionPerformed

    private void myRenderMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_myRenderMenuItemActionPerformed
        renderNetwork(bnc);
    }//GEN-LAST:event_myRenderMenuItemActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                frame = new MainJFrame();
                frame.setSize(dimension);
                frame.setPreferredSize(dimension);
                frame.setVisible(true);
            }
        });
   
    }

    
    public static Dimension dimension = new Dimension(1000, 800);
    public static MainJFrame frame;
    public static boolean deleteMode = false;
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu jMenu1;
    private javax.swing.JDialog myAddErrorMessage;
    private javax.swing.JLabel myAddErrorMessageLabel;
    private javax.swing.JButton myAddErrorMessageOKButton;
    private javax.swing.JPanel myAddErrorMessagePanel;
    private javax.swing.JMenuItem myAddMenuItem;
    private javax.swing.JDialog myAddNodeDialog;
    private javax.swing.JButton myCancelButton;
    private javax.swing.JButton myConfirmButton;
    private javax.swing.JCheckBoxMenuItem myDeleteMenuItem;
    private javax.swing.JMenu myEditMenu;
    private javax.swing.JFileChooser myFileChooser;
    private javax.swing.JDialog myFileDialog;
    private javax.swing.JMenu myFileMenu;
    private gui.MyGraphPanel myGraphPanel;
    private javax.swing.JRadioButtonMenuItem myHierarchicalMenuItem;
    private javax.swing.JMenuItem myLoadMenuItem;
    private javax.swing.JMenuBar myMenuBar;
    private javax.swing.JLabel myNameLabel;
    private javax.swing.JTextField myNameTextField;
    private javax.swing.JRadioButtonMenuItem myParallelEdgeMenuItem;
    private javax.swing.JLabel myParamsLabel;
    private javax.swing.JTextField myParamsTextField;
    private javax.swing.JRadioButtonMenuItem myPartitionMenuItem;
    private javax.swing.JMenuItem myRenderMenuItem;
    private javax.swing.JMenuItem mySaveMenuItem;
    private javax.swing.JCheckBoxMenuItem mySelectMenuItem;
    private javax.swing.JComboBox myTypeComboBox;
    private javax.swing.JLabel myTypeLabel;
    // End of variables declaration//GEN-END:variables
}
